<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户详情</title>
    <link rel="stylesheet" href="{{ url_for('static',filename='layuimini/lib/layui-v2.6.3/css/layui.css') }}" media="all">
    <link rel="stylesheet" href="{{ url_for('static',filename='layuimini/css/layuimini.css') }}" media="all">
    <link rel="stylesheet" href="{{ url_for('static',filename='layuimini/css/themes/default.css') }}" media="all">
    <link rel="stylesheet" href="{{ url_for('static',filename='layuimini/lib/font-awesome-4.7.0/css/font-awesome.min.css') }}" media="all">
    <style>
        .div-content{
            background-color: white;
            border-radius: 5px;
            padding: 14px;
        }
        .table-search-fieldset{
            margin: 0;
            border: 1px solid #e6e6e6;
            padding: 10px 20px 5px 20px;
            color: #6b6b6b;
        }

    </style>
</head>
<body>
<div class="div-content">
    <form class="layui-form layui-form-pane" action="" id="form-search">
        <fieldset class="table-search-fieldset">
            <legend>搜索条件</legend>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 100px">用户编号</label>
                    <div class="layui-input-inline" style="width: 120px;">
                        <input type="text" name="userid" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 100px">用户名</label>
                    <div class="layui-input-inline" style="width: 120px;">
                        <input type="text" name="username" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <button id="btn-search" lay-submit lay-filter="btn-search-filter" type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal">
                        <i class="layui-icon layui-icon-search"></i>
                        搜索
                    </button>
                    <button id="btn-reset" type="reset" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-primary">
                        <i class="layui-icon layui-icon-fonts-clear"></i>
                        清空条件
                    </button>
                </div>

            </div>
        </fieldset>
    </form>

    <table id="table-booklist" lay-filter="table-booklist-filter"></table>
</div>
<script src="{{ url_for('static',filename='layuimini/lib/layui-v2.6.3/layui.js') }}" charset="utf-8"></script>
<script src="{{ url_for('static',filename='js/jquery-3.6.0.min.js') }}" charset="utf-8"></script>

<script type="text/html" id="col-book-bar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>

    function refreshTable(){
        $("#btn-reset").click();
        $("#btn-search").click();
    }

    layui.use(['laydate','table','form'],function () {
        var laydate=layui.laydate;
        var table=layui.table;
        var form=layui.form;

        //执行一个laydate实例
        laydate.render({
            elem:".date"
        });

        //用户列表表格渲染
        table.render({
            elem: '#table-booklist'
            ,url: '/admin/user/searchAll' //数据接口
            ,page: true //开启分页
            ,toolbar:"#table-book-toolbar"
            ,limits:[6,8,10,20]
            ,cols: [[ //表头
                {type:"checkbox",width: 60},
                {field: 'userid', title: '学号', width:200,align:'center'}
                ,{field: 'username', title: '姓名', width:150,align:'center'}
                ,{field: 'useremail', title: '电子邮件', width:200,align:'center'}
                ,{field: 'userphone', title: '电话',width:200,align:'center'}
                ,{title: '操作',align:'center',toolbar: '#col-book-bar'}
            ]]
        });

        //监听表单提交
        form.on('submit(btn-search-filter)', function(data){
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            let substr="";
            let arr={"userid":data.field.userid, "username":data.field.username};
            for(let i in arr){
                if(arr[i]===""){delete arr[i]}
            }
            for(let j in arr){substr+="&"+j+"="+arr[j];}
            substr=substr.substring(1);

            table.render({
                elem: '#table-booklist'
                ,url: '/admin/user/searchAll' //数据接口
                ,page: true //开启分页
                ,toolbar:"#table-book-toolbar"
                ,limits:[6,8,10,20]
                ,cols: [[ //表头
                    {type:"checkbox",width: 60},
                    {field: 'userid', title: '学号', width:200,align:'center'}
                    ,{field: 'username', title: '姓名', width:150,align:'center'}
                    ,{field: 'useremail', title: '电子邮件', width:200,align:'center'}
                    ,{field: 'userphone', title: '电话',width:200,align:'center'}
                    ,{title: '操作',align:'center',toolbar: '#col-book-bar'}
                ]]
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //操作列
        table.on('tool(table-booklist-filter)', function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            console.log(data.userid)
            if(layEvent=='del') {
                layer.confirm('您确认删除该记录吗?', {icon: 3, title: '删除确认'}, function (index) {
                    //异步请求，删除数据
                    $.ajax({
                        url: "/admin/user/delete/" + data.userid,
                        success: function (response) {
                            if (response.state == 200) {
                                layer.msg(response.message, {icon: 1});
                                refreshTable();
                            } else {
                                layer.alert("删除失败！", {icon: 2, anim: 6});
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>
</body>
</html>